package com.hxjb.config;

import com.hxjb.common.BaseDao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * Created by wt on 2018/4/19.
 */
@Configuration
@EnableTransactionManagement
public class BasedaoConfig implements TransactionManagementConfigurer {
    @Autowired
    private DataSource dataSource;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws SQLException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(dataSource);

//        //分页插件
//        PageHelper pageHelper = new PageHelper();
//        Properties properties = new Properties();
////        properties.setProperty("reasonable", "true");
////        properties.setProperty("supportMethodsArguments", "true");
////        properties.setProperty("returnPageInfo", "check");
////        properties.setProperty("params", "count=countSql");
//        properties.setProperty("helperDialect", "mysql");
//        pageHelper.setProperties(properties);
//
//        //添加插件
//        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});

        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
//            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mybatis/*/*.xml"));
            return sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }


    @Bean(name = "baseDao")
    public BaseDao baseDao() throws Exception {
        return new BaseDao(sqlSessionFactory());
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);

    }

//    @Bean(name = "hxjbAppSqlSessionFactory")
//    public SqlSessionFactory hxjbAppSqlSessionFactory(@Qualifier("hxjbAppDataSource") DataSource hxjbAppDataSource)
//            throws Exception {
//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(hxjbAppDataSource);
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
////        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
////                .getResources(mapperLocations));
//        sessionFactory.setConfigLocation(resolver.getResource(mybatisProperties.getConfigLocation()));
//        sessionFactory.setTypeAliasesPackage(dbProperties.getTypeAliasesPackage());
//        return sessionFactory.getObject();
//    }
//
//
//    @Bean(name = "hxjbAppBaseDao")
//    public BaseDao hxjbAppBaseDao() throws Exception {
//        return new BaseDao(hxjbAppSqlSessionFactory(hxjbAppDataSource()));
//    }

}
